package com.itheima.mall.web.ums;


import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.ums.RoleService;
import com.itheima.mall.entity.domain.dto.RoleWithMenuDto;
import com.itheima.mall.entity.domain.dto.RoleWithResourceDto;
import com.itheima.mall.entity.domain.ums.Menu;
import com.itheima.mall.entity.domain.ums.Resource;
import com.itheima.mall.entity.domain.ums.Role;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/role")
@CrossOrigin(origins = "*")
public class RoleController {

    @DubboReference
    private RoleService roleService;

    @PostMapping("/allocMenu")
    public Result allocMenu(@RequestParam RoleWithMenuDto roleWithMenuDto){
        roleService.allocMenu(roleWithMenuDto);
        return Result.success();
    }

    @PostMapping("/allocResource")
    public Result allocResource(@RequestParam RoleWithResourceDto roleWithResourceDto){
        roleService.allocResource(roleWithResourceDto);
        return Result.success();
    }

    @PostMapping("/create")
    public Result create(@RequestBody Role role){
        roleService.create(role);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam List<Integer> ids){
        roleService.delete(ids);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(String keyword, Integer pageNum, Integer pageSize){
        PageResult pageResult = roleService.listR(keyword,pageNum,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/listAll")
    public Result listAll(){
        List<Role> roles = roleService.listAll();
        return Result.success(roles);
    }

    @GetMapping("/listMenu/{roleId}")
    public Result listMenu(@PathVariable Integer roleId){
        List<Menu> menus = roleService.listMenu(roleId);
        return Result.success(menus);
    }

    @GetMapping("/listResource/{roleId}")
    public Result listResource(@PathVariable Integer roleId){
        List<Resource> resources = roleService.listResource(roleId);
        return Result.success(resources);
    }

    @PostMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Role role){
        roleService.updateR(role);
        return Result.success();
    }

    @PostMapping("/updateStatus/{id}")
   public Result updateStatus(@PathVariable Integer id, Integer status){
        roleService.updateStatus(id,status);
        return Result.success();
    }
}

